package com.rt.easyjava.common.utils.others;

import org.dom4j.*;
import org.dom4j.tree.DefaultElement;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@SuppressWarnings({"rawtypes", "unchecked"})
public class XmlUtils {

    public static void main(String[] args) throws DocumentException {
        String xmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<serviceinfo><param name=\"authcode\">ZOjYjohamjV7kVM/7L/y5Fu/oRKZhVpX8KvxQsmjBGk=</param><param name=\"pageindex\">1</param><param name=\"pagesize\">10</param><param name=\"count\">11</param><param name=\"pagecount\">2</param><param name=\"orderBy\"></param><param name=\"errorcode\">0</param><param name=\"errormsg\">查询成功！</param><result><metadata><qsmc v=\"南浦大桥\"/><gxsj v=\"2020-09-27 14:28:00\"/><qssd v=\"48\"/></metadata><metadata><qsmc v=\"复兴东路隧道东向西\"/><gxsj v=\"2020-09-27 14:29:27\"/><qssd v=\"12\"/></metadata><metadata><qsmc v=\"复兴东路隧道西向东\"/><gxsj v=\"2020-09-27 14:29:27\"/><qssd v=\"17\"/></metadata><metadata><qsmc v=\"大连路隧道北向南\"/><gxsj v=\"2020-09-27 14:29:27\"/><qssd v=\"22\"/></metadata><metadata><qsmc v=\"大连路隧道南向北\"/><gxsj v=\"2020-09-27 14:29:27\"/><qssd v=\"22\"/></metadata><metadata><qsmc v=\"延安东路隧道东向西\"/><gxsj v=\"2020-09-27 14:29:27\"/><qssd v=\"22\"/></metadata><metadata><qsmc v=\"延安东路隧道西向东\"/><gxsj v=\"2020-09-27 14:29:27\"/><qssd v=\"16\"/></metadata><metadata><qsmc v=\"打浦路隧道南向北\"/><gxsj v=\"2020-09-27 14:19:27\"/><qssd v=\"33\"/></metadata><metadata><qsmc v=\"新建路隧道北向南\"/><gxsj v=\"2020-09-27 14:29:27\"/><qssd v=\"12\"/></metadata><metadata><qsmc v=\"新建路隧道南向北\"/><gxsj v=\"2020-09-27 14:29:27\"/><qssd v=\"10\"/></metadata></result></serviceinfo>";
        System.out.println(xml2Map(xmlContent));
    }

    public static Map xml2Map(String xml) throws DocumentException {
        Map<String, Object> resMap = new HashMap<>();
        Document doc = DocumentHelper.parseText(xml);
        Element root = doc.getRootElement();
        List<Element> eles = root.elements();
        if (null == eles) {
            resMap.put(root.attribute("name").getValue(), "");
        }
        for (Element ele : eles) {
            leafNode2Map(resMap, ele);
        }
        return resMap;
    }

    private static void leafNode2Map(Map<String, Object> map, Element node) {
        List<Element> elements = node.elements();
        if ("result".equals(node.getQName().getName())) {
            //result
            List<Node> list = node.content();
            List<Map> result = new ArrayList<>();
            for (Node node1 : list) {
                //metadata
                Map<String, String> childContent = new HashMap<>();
                Element element = (Element) node1;
                List<Map> metaData = new ArrayList();
                //meta内部标签
                for (Node node2 : element.content()) {
                    Element element1 = (DefaultElement) node2;
                    childContent.put(element1.getName(), ((DefaultElement) element1).attribute(0).getValue());
                }
                result.add(childContent);
            }
            map.put("result", result);
        } else if (elements == null || elements.size() == 0) {
            map.put(node.attributeValue("name"), node.getData());
        } else {
            for (Element element : elements) {
                // 递归
                leafNode2Map(map, element);
            }
        }
    }
}


